package handler

import (
	"Gin/api/request"
	"Gin/global/model"
	"Gin/global/pkg"
	context2 "context"
	"github.com/gin-gonic/gin"
	"net/http"
	"serve/videoSrv"
)

func UserLogin(context *gin.Context) {
	var UserLoginRequest request.UserLoginRequest
	if err := context.ShouldBind(&UserLoginRequest); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
		})
		return
	}
	login, err := model.VideoClient.UserLogin(context2.Background(), &videoSrv.UserLoginRequest{
		Account:  UserLoginRequest.Account,
		Password: UserLoginRequest.Password,
	})
	if err != nil {
		context.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
		})
		return
	}
	token, err := pkg.GetToken(login.UserId, 1)
	if err != nil {
		context.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
		})
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "登录成功",
		"data":    token,
	})

}
